package com.dys.designpattern.factorymethod.applyaward.factory;

import com.dys.designpattern.factorymethod.applyaward.factory.impl.DiscountFreeGoodsFactory;
import com.dys.designpattern.factorymethod.applyaward.factory.impl.SmallGiftFreeGoodsFactory;
import com.dys.designpattern.factorymethod.applyaward.factory.impl.YouKuMemberFreeGoodsFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: 疾风
 * @ClassName: FreeGoodsFactoryMap
 * @Description: 用简单工厂模式实现: 工厂的工厂,作用是不需要每次创建新的工厂对象
 * @CreateDate: 2024/05/14
 * @Version: v1.0.0
 */
public class FreeGoodsFactoryMap {

    private static final Map<Integer, FreeGoodsFactory> CACHED_FACTORIES = new HashMap<>();

    static {
        CACHED_FACTORIES.put(1, new DiscountFreeGoodsFactory());
        CACHED_FACTORIES.put(2, new YouKuMemberFreeGoodsFactory());
        CACHED_FACTORIES.put(3, new SmallGiftFreeGoodsFactory());
    }

    public static FreeGoodsFactory getParserFactory(int type) {
        FreeGoodsFactory freeGoodsFactory = null;
        if (type == 1) {
            freeGoodsFactory = CACHED_FACTORIES.get(1);
            return freeGoodsFactory;
        } else if (type == 2) {
            freeGoodsFactory = CACHED_FACTORIES.get(2);
            return freeGoodsFactory;
        } else {
            freeGoodsFactory = CACHED_FACTORIES.get(3);
            return freeGoodsFactory;
        }
    }

    public static FreeGoodsFactory getParserFactory2(int type) {
        FreeGoodsFactory freeGoodsFactory = null;
        freeGoodsFactory = CACHED_FACTORIES.get(type);
        return freeGoodsFactory;
    }
}
